package com.fqy.thread02.number;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * User: fqy
 * Date: 2021/9/1
 * Time: 14:57
 * Describe: i++不安全问题
 */
public class MyNumber {
//    private Integer num = 0;
//  方式三：CAS
    AtomicInteger num = new AtomicInteger(0);

    public AtomicInteger getNum() {
        return num;
    }
    public void setNum() {
        num.incrementAndGet();
    }

    /*
    方式一：synchronized
    public synchronized void setNum() {
        num++;
    }*/
    /*
    方式二：Lock
    Lock lock = new ReentrantLock();
    public void setNum() {
        lock.lock();
        try {
            num++;
        }finally {
            lock.unlock();
        }
    }*/
}
